﻿using DataBaseDAO;
using Microsoft.AspNetCore.Mvc;

namespace MyCSharpMVCProject.Controllers
{
    public class LogController : BaseController
    {
        private UserDAO userDAO = new UserDAO();
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string username, string password)
        {
            //如果账号或者密码有空，直接返回失败
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return ErrorResult("缺少必要参数，请求失败");
            }
            else
            {
                //调用用户登录的方法
                var data = userDAO.CheckLogin(username, password);
                if (data != null)
                {
                    if (data.Power == "进货员")
                    {
                        return SuccessResult("登录成功");
                    }
                    else if(data.Power == "管理员")
                    {
                        return Success_ManagerResult("管理员登录成功");
                    }
                    else
                    {
                        return ErrorResult("登录失败");
                    }
                }
                else
                {
                    return ErrorResult("登录失败");
                }
            } 
        }
        public string GetUserList(string keyWord)
        {
            var pageData = userDAO.GetUsersList(keyWord);
            return pageData.ToJsonCon();
        }
    }
}
